<?php

define('DS', DIRECTORY_SEPARATOR);
define('PS', PATH_SEPARATOR);
define('BP', dirname(__FILE__) . DS);

include_once BP . 'lib/Kd/Loader.php';
include_once BP . 'lib/Kd/Autoload.php';

Kd_Loader::addIncludePath(BP . 'lib' . DS);
Kd_Autoload::register();

Kd_Web::getSession()->start();

function random($len) {
    $srcstr = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
    mt_srand();
    $strs = "";
    for ($i = 0; $i < $len; $i++) {
        $strs.=$srcstr[mt_rand(0, 33)];
    }
    return strtoupper($strs);
}

$str = random(4);
$width = 160;
$height = 50;
$fontname = BP.'courbi.ttf';
@header("Content-Type:image/png");
$im = imagecreate($width, $height);
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$pix = imagecolorallocate($im, 187, 230, 247);
$fontcolor = imagecolorallocate($im, 41, 200, 238);
mt_srand();
for ($i = 0; $i < 1000; $i++) {
    imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}
//imagestring($im, 20, 7, 5, $str, $font);
for($i = 0; $i < strlen($str); $i++){
    imagettftext($im, 32, mt_rand(-20, 20), 25 * ($i + 1), 38, $fontcolor, $fontname, $str[$i]);
}
imagerectangle($im, 0, 0, $width - 1, $height - 1, $fontcolor);
imagepng($im);
imagedestroy($im);
Kd_Web::getSession()->set('validate_code', $str);